
function initialize() {
  var map = new GMap2(document.getElementById("content"));
  map.addControl(new GLargeMapControl());
  // ann arbor = (42.28073800051147, -83.75444412231445)
  map.setCenter(new GLatLng(42.28073800051147, -83.75444412231445), 13);

	var geocoder = new GClientGeocoder();

	$.getJSON("listings",
  	function(data){
			process(map, geocoder, data);
	});
}

function linkify(title, url) {
	return "<a href = \"" + url + "\">" + title + "</a>";
}

function linkifyW(title, url, px) {
	return "<a style=\"width: " + px + "px; word-wrap: break-word\" href = \"" + url + "\">" + title + "</a>";
}


function img(url) {
  return "<img width=100 height=100 src=\"" + url + "\">";
}

function photostable(photos) {
  str = "<table>";
  
  for (i = 0; i < photos.length; i++) {
    if ((i % 4) === 0) {
      str = str + "<tr>";
    }
    str = str + "<td>" + img(photos[i]) + "</td>";
  }
  return str + "</table>";
}

function infoWindow(item) {
  px = item.photos.length * 100;
  if (px < 200) {
    px = 200;
  }
  return linkifyW(item.title, item.url, px) + photostable(item.photos) + item.address;
}

// each item has properties:
// - address
// - lat
// - long
// - title
// - url
// - date
// - photos
// this will see if the item is already geocoded, and if not make the geocoding
// request locally.  finally, it will place a marker on the map
function process(map, geocoder, items) {
	var numGeocoded = 0;
	var numSuccess = 0;
	var numLocalCoded = 0;
	
	function showAddressAll() {
		if (numGeocoded < items.length) {
		  var item = items[numGeocoded];
      if (item.lat != undefined && item.longit != undefined) {
        placeMarker(item, item.lat, item.longit);
        numGeocoded += 1;
        numSuccess += 1;
        showAddressAll();        
      } else {
	      geocoder.getLocations(item.address, addressResolved);				
		  }
		} else {
		  // done
		  $("div#footer p").append("had to locally geocode " + numLocalCoded + " out of " + items.length + " items");		    
		}
	}		
	
  function addressResolved(response) {
     var delay = 0;
     var item = items[numGeocoded];
     if (response.Status.code == 620) {
       // Too fast, try again, with a small pause
       delay = 500;
     } else {
       if (response.Status.code == 200) {
         // Success; do something with the address.
         place = response.Placemark[0];
         placeMarker(item, place.Point.coordinates[1], place.Point.coordinates[0]);
         numSuccess += 1;
         numLocalCoded += 1;
       } else {
	       // some other kind of geocoding error
  			 item.uncoded_reason = "geocoding error on address \"" + item.address + "\"" + "with status code " + response.Status.code;
				 addItemUnparsed(item);							
			}
       // Move onto the next address; this skips bad addresses, too.
       numGeocoded += 1;
     }
     setTimeout(showAddressAll, delay);
   }
   
   function placeMarker(item, lat, longit) {
     var point = new GLatLng(lat, longit);
     var marker = new GMarker(point);
     GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(infoWindow(item));
     });
     map.addOverlay(marker);
   }
   
   showAddressAll();
}


